<?php

class User_list extends CI_Controller {

	public function __construct() {
		parent::__construct();

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->model('user');
	}
	
	public function _remap($method, $params)
	{
		$u = Current_User::user();
		if(!$u) {
			$this->load->view('pages/user/logout', array('info' => '请先登录系统'));
		}else{
			$role = Current_User::my_role();
			if($role == Current_User::$administrtor) {
				// $this->$method();
				if (method_exists($this, $method))
			    {
			        return call_user_func_array(array($this, $method), $params);
			    }
			}
			else redirect("/home/nopower");
		}
	}

	public function index() {
		$ret = $this->user->get_users();
		$i = 0;
		foreach ($ret as $user){
			$ret[$i]->sex = $user->sex == 'male' ? '男' : '女';
			$role = $this->user->get_role($user->role_id);
			$ret[$i]->role_id = $role ? $role->name : '';
			$i++;
		}
		$data = array('users' => $ret);
		$this->load->view('pages/user/list_form', $data);
	}
	
	public function edit($user_id) {
		if($user_id) {
			$ret = $this->user->get_roles();
			$data = array();


			foreach ($ret as $role){
				$data[$role->id] = $role->name;
			}
			
			$u = $this->user->get_user($user_id);
			$this->load->view('pages/user/edit_form', array('userObj' => $u, 'roles' => $data));
		}
	}
	
	public function del($user_id) {
		if($user_id) {			
			//$u = $this->doctrine->em->find('Entities\User', $user_id);
			//$ret = $this->doctrine->em->remove($u);
			//$ret2 = $this->doctrine->em->flush();
			$this->db->delete('user', array('user_id' => $user_id)); 
			redirect("user/user_list");
		}
	}
	
}
